package main

import (
	"bytes"
	"github.com/jchv/go-webview2"
	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"
	"log"
)

func main() {
	// 创建WebView实例
	webview := webview2.New(true)

	defer webview.Destroy()

	// 创建一个基本的HTML文档结构

	doc := &html.Node{
		Type:     html.DocumentNode,
		DataAtom: atom.Html,
		Data:     "html",
	}

	headNode := &html.Node{
		Type: html.ElementNode,
		Data: "head",
		Attr: []html.Attribute{},
	}
	doc.AppendChild(headNode)
	linkElementPlus := &html.Node{
		Type: html.ElementNode,
		Data: "link",
		Attr: []html.Attribute{
			{Key: "rel", Val: "stylesheet"},
			{Key: "href", Val: "./element-plus.css"},
		},
	}
	headNode.AppendChild(linkElementPlus)
	// 引入Vue和Element Plus的CDN（这里只是示例，实际应用中可能需要更好的资源管理方式）
	scriptVue := &html.Node{
		Type: html.ElementNode,
		Data: "script",
		Attr: []html.Attribute{
			{Key: "src", Val: "./vue.js"},
		},
	}
	headNode.AppendChild(scriptVue)

	scriptElementPlus := &html.Node{
		Type: html.ElementNode,
		Data: "script",
		Attr: []html.Attribute{
			{Key: "src", Val: "./element-plus.js"},
		},
	}
	headNode.AppendChild(scriptElementPlus)

	bodyNode := &html.Node{
		Type: html.ElementNode,
		Data: "body",
		Attr: []html.Attribute{
			{Key: "style", Val: "margin: 0; padding: 0;"},
		},
	}
	doc.AppendChild(bodyNode)

	// 创建一个简单的Vue应用示例（这里只是基本结构，你需要根据实际需求扩展）
	appDiv := &html.Node{
		Type: html.ElementNode,
		Data: "div",
		Attr: []html.Attribute{
			{Key: "id", Val: "app"},
			{Key: "style", Val: "width: 100%; height: 100%;"},
		},
	}
	bodyNode.AppendChild(appDiv)

	scriptApp := &html.Node{
		Type: html.ElementNode,
		Data: "script",
		Attr: []html.Attribute{},
	}
	scriptApp.AppendChild(&html.Node{
		Type: html.TextNode,
		Data: `
        const { createApp } = Vue
        createApp({
            data() {
                return {
                    message: 'Hello, Vue with Element Plus!'
                }
            }
        }).mount('#app')
        `,
	})
	bodyNode.AppendChild(scriptApp)

	// 将生成的HTML文档转换为字符串
	var buf bytes.Buffer
	err := html.Render(&buf, doc)
	if err != nil {
		log.Fatal(err)
	}
	htmlStr := buf.String()

	// 导航到临时HTML文件
	webview.SetHtml(htmlStr)

	// 运行WebView，显示窗口并处理事件
	webview.Run()
}
